package com.yixing.tech.wcs.event;

import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.wcs.service.IBoxTaskService;
import com.yixing.tech.wcs.service.ICRUDService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 设备回复事件监听器
 * */
@Component
@Slf4j
public class DeviceEventListener implements ApplicationListener<DeviceReplyEvent> {

    @Resource
    IBoxTaskService boxTaskService;

    @Resource
    ICRUDService crudService;

    static final ThreadPoolTaskExecutor singleTaskThreadPool;

    static {
        singleTaskThreadPool = new ThreadPoolTaskExecutor();
        singleTaskThreadPool.setCorePoolSize(1);
        singleTaskThreadPool.setMaxPoolSize(1);
        singleTaskThreadPool.setQueueCapacity(1);
        singleTaskThreadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
        singleTaskThreadPool.setThreadNamePrefix("取出料箱线程");
        singleTaskThreadPool.initialize();
    }

    @Override
    public void onApplicationEvent(DeviceReplyEvent event) {
        DeviceEventMessage message = event.getDeviceInfo();

        if("81".equals(message.getEventName())) {
            handleDeviceState(message);
        }else if("83".equals(message.getEventName())) {
            handleReplyPutDownFin(message);
        }else if("84".equals(message.getEventName())) {
            handleReplyHandlingFin(message);
        }else if("86".equals(message.getEventName())) {
            handleReplyTakeOutFin(message);
        }
    }

    private void handleDeviceState(DeviceEventMessage message) {
        if(message.getEnablePutDown()) {
            try {
                boxTaskService.enablePutDown();
            } catch (BusinessException e) {
                log.error("设备允许放下料箱处理逻辑抛出异常:{}", e);
            }
        }
        if(message.getEnableTakeOut()) {
            try {
                String podCode = message.getPodCode2();
//                Map podMap = crudService.findPodByPodCode(podCode);
//                if(ObjectUtils.isNotEmpty(podMap)) {
//                    boxTaskService.enableTakeOut(podCode);
//                }
                if(ObjectUtils.isNotEmpty(podCode)) {
                    singleTaskThreadPool.submit(() -> {
                        try {
                            boxTaskService.enableTakeOut(podCode);
                        }catch (Exception ex) {
                            log.error("设备允许取出料箱逻辑抛出异常:podCode[{}],ex[{}]", podCode, ex);
                        }
                    });
                }
            } catch (Exception e) {
                log.error("设备允许取出料箱逻辑抛出异常:{}", e);
            }
        }

        if(ObjectUtils.isNotEmpty(message.getPodCode1())) {
            try {
                String podCode = message.getPodCode1();
                Map podMap = crudService.findPodByPodCode(podCode);
                if(ObjectUtils.isNotEmpty(podMap)) {
                    crudService.updateCameraPodCode(podCode);
                }else {
                    crudService.updateCameraPodCode("");
                }
            } catch (BusinessException e) {
                log.error("设备读取到料箱码更新料箱码抛出异常:{}", e);
            }
        }
    }

    private void handleReplyPutDownFin(DeviceEventMessage message) {
        try {
            boxTaskService.replyPutDownFin();
        } catch (BusinessException e) {
            log.error("设备回复收到放下料箱完成信息处理抛出异常:{}", e);
        }
    }

    private void handleReplyHandlingFin(DeviceEventMessage message) {
        try {
            boxTaskService.replyBoxHandlingFin();
        } catch (BusinessException e) {
            log.error("设备回复收到料箱处理完成信息抛出异常:{}", e);
        }
    }

    private void handleReplyTakeOutFin(DeviceEventMessage message) {
        try {
            boxTaskService.replyTakeOutFin();
        } catch (BusinessException e) {
            log.error("设备回复收到料箱取出完成信息抛出异常:{}", e);
        }
    }
}
